<?php
namespace backend\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use backend\models\ModelListModel;
use backend\models\ModelFieldsModel;
use yii\filters\VerbFilter;
use yii\data\Pagination;

/**
 * Site controller
 */
class ModelController extends CommonController
{
	/**
	* @inheritdoc
	*/
	public function behaviors()
	{
		return [
		];
	}

	/**
	* @inheritdoc
	*/
	public function actions()
	{
		return [
		'error' => [
		'class' => 'yii\web\ErrorAction',
		],
		];
	}

	/**
	* 模型列表
	* @return Json
	*/
	public function actionIndex()
	{
		$search=array();
		$search['page']=intval($this->getParam('page','1'));
		$result=ModelListModel::_search($search);
		return $this->render('list',$result);
	}

	/**
	 * 模块的添加和修改
	 * @return JSON
	 */
	public function actionEdit()
	{
		header('Content-type: application/json');
		if (!empty($_POST['submit'])) {
			$data['id']=intval($this->getParam('id',0));
			if (isset($_POST['status']) && $_POST['status'] != '') { $data['status']=intval($this->getParam('status',0));}
			if (isset($_POST['title']) && $_POST['title'] != '') {$data['title']=trim($this->getParam('title',""));}
			$result=ModelListModel::_edit($data);
			$this->out(Self::OK, Self::OK_TXT,$result);
			exit;
		}

		$id=intval($this->getParam('id','',"模型ID是必须存在的"));
		$result=ModelListModel::findIdentity($id);
		if (empty($result)) {
			$this->out(Self::EXP_EMPTY, Self::EXP_EMPTY_TXT);
		}
		$result['updated_at']=date('Y-m-d H:i:s',$result['updated_at']);
		$this->out(Self::OK, Self::OK_TXT,$result);
	}

	public function actionListStatus()
	{
		$id=intval($this->getParam('id','',"模型ID是必须存在的"));
		$result=ModelListModel::ListStatus($id);
		if ($result) {
			$this->out(Self::OK, Self::OK_TXT);
		}else{
			$this->out(Self::EXP_EMPTY, Self::EXP_EMPTY_TXT);
		}

	}
	/**
	* 用户信息读取
	* @return [type] [description]
	*/
	public function actionUserInfo()
	{
		$id=intval($this->getParam('id','',"ID是必须存在的"));
		$info=User::findIdentity($id);
		if (!empty($info)) {
			return print_r($info->attributes);
		}else{
			$this->out(Self::EXP_EMPTY, Self::EXP_EMPTY_TXT,'','PAGE');
		}

	}
	/**
	* 用户信息修改
	* @return [type] [description]
	*/
	public function actionUserEdit()
	{

	}
}
